package com.logicalthinking.socialmall.biz;

import com.github.chenlijia1111.commonModule.biz.ShoppingOrderBiz;
import com.github.chenlijia1111.commonModule.common.requestVo.order.*;
import com.github.chenlijia1111.commonModule.common.responseVo.product.GoodVo;
import com.github.chenlijia1111.commonModule.entity.ShoppingOrder;
import com.github.chenlijia1111.commonModule.service.GoodsServiceI;
import com.github.chenlijia1111.commonModule.service.IdGeneratorServiceI;
import com.github.chenlijia1111.commonModule.service.ProductServiceI;
import com.github.chenlijia1111.commonModule.service.ShoppingOrderServiceI;
import com.github.chenlijia1111.utils.common.Result;
import com.github.chenlijia1111.utils.core.JSONUtil;
import com.github.chenlijia1111.utils.core.StringUtils;
import com.github.chenlijia1111.utils.list.Lists;
import com.logicalthinking.socialmall.common.vo.requestVo.order.SocialOrderAddParams;
import com.logicalthinking.socialmall.entity.OrderAppend;
import com.logicalthinking.socialmall.entity.ProductAppend;
import com.logicalthinking.socialmall.service.OrderAppendServiceI;
import com.logicalthinking.socialmall.service.ProductAppendServiceI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 订单
 *
 * @author chenlijia
 * @version 1.0
 * @since 2019/11/29 0029 上午 9:39
 **/
@Service
public class OrderBiz {

    @Autowired
    private ShoppingOrderBiz shoppingOrderBiz;
    @Autowired
    private ShoppingOrderServiceI shoppingOrderService;//订单
    @Autowired
    private OrderAppendServiceI orderAppendService;//订单补充
    @Autowired
    private GoodsServiceI goodsService;//商品
    @Autowired
    private ProductServiceI productService;//产品
    @Autowired
    private ProductAppendServiceI productAppendService;//产品补充

    @Autowired
    @Qualifier("groupIdGeneratorService")
    private IdGeneratorServiceI groupIdGeneratorServiceImpl;
    @Autowired
    @Qualifier("ShoppingOrderNoGeneratorService")
    private IdGeneratorServiceI shoppingIdGeneratorServiceImpl;
    @Autowired
    @Qualifier("sendOrderNoGeneratorService")
    private IdGeneratorServiceI sendOrderIdGeneratorServiceImpl;
    @Autowired
    @Qualifier("receiveOrderNoGeneratorService")
    private IdGeneratorServiceI receiveOrderIdGeneratorServiceImpl;
    @Autowired
    @Qualifier("shopGroupIdGeneratorService")
    private IdGeneratorServiceI shopGroupIdGeneratorServiceImpl;

    @Transactional
    public Result addOrder(SocialOrderAddParams params) {
        Result result = shoppingOrderBiz.add(params, groupIdGeneratorServiceImpl, shoppingIdGeneratorServiceImpl,
                sendOrderIdGeneratorServiceImpl, receiveOrderIdGeneratorServiceImpl, shopGroupIdGeneratorServiceImpl);
        if (result.getSuccess()) {
            //添加交易类型
            Object data = result.getData();
            if (Objects.nonNull(data)) {
                String groupId = (String) data;
                List<ShoppingOrder> shoppingOrders = shoppingOrderService.listByCondition(new ShoppingOrder().setGroupId(groupId));
                if (Lists.isNotEmpty(shoppingOrders)) {
                    List<OrderAppend> orderAppends = shoppingOrders.stream().map(e -> {
                        OrderAppend orderAppend = new OrderAppend();
                        orderAppend.setOrderNo(e.getOrderNo());
                        orderAppend.setTransactionType(params.getTransactionType());
                        return orderAppend;
                    }).collect(Collectors.toList());
                    orderAppendService.batchAdd(orderAppends);
                }
            }
        }
        return result;
    }


    /**
     * 试算订单金额
     *
     * @param params 1
     * @return com.github.chenlijia1111.utils.common.Result
     * @since 下午 4:20 2019/11/22 0022
     **/
    public Result calculatePrice(OrderAddParams params) {
        //查询订单商品的物流费
        List<SingleOrderAddParams> singleOrderList = params.getSingleOrderList();
        Set<String> goodsIdSet = singleOrderList.stream().map(e -> e.getGoodId()).collect(Collectors.toSet());
        List<GoodVo> goodVos = goodsService.listByGoodIdSet(goodsIdSet);
        Set<String> productIdSet = goodVos.stream().map(e -> e.getProductId()).collect(Collectors.toSet());
        List<ProductAppend> productAppends = productAppendService.listByProductIdSet(productIdSet);
        //构建物流券用于计算
        List<CouponWithGoodIds> couponWithGoodIdsList = params.getCouponWithGoodIdsList();
        if (Objects.isNull(couponWithGoodIdsList)) {
            couponWithGoodIdsList = new ArrayList<>();
        }

        for (SingleOrderAddParams singleOrderAddParams : singleOrderList) {
            String goodId = singleOrderAddParams.getGoodId();
            Optional<GoodVo> any = goodVos.stream().filter(e -> Objects.equals(e.getId(), goodId)).findAny();
            if (any.isPresent()) {
                GoodVo goodVo = any.get();
                String productId = goodVo.getProductId();
                Optional<ProductAppend> any1 = productAppends.stream().filter(e -> Objects.equals(e.getProductId(), productId)).findAny();
                if (any1.isPresent()) {
                    ProductAppend productAppend = any1.get();
                    //物流券
                    String freightCoupon = productAppend.getFreightCoupon();
                    if (StringUtils.isNotEmpty(freightCoupon)) {
                        String couponId = JSONUtil.strToJsonNode(freightCoupon).get("id").asText();
                        //获取物流券Id
                        CouponWithGoodIds couponWithGoodIds = new CouponWithGoodIds();
                        couponWithGoodIds.setCouponId(couponId);
                        couponWithGoodIds.setGoodIdList(Lists.asList(goodId));
                        couponWithGoodIdsList.add(couponWithGoodIds);
                    }
                }
            }
        }
        return shoppingOrderBiz.calculatePrice(params);
    }

    /**
     * 客户取消订单
     * 客户主动取消订单,只能是待支付的订单
     * 并且是组订单全部取消
     *
     * @param groupId 1
     * @return com.github.chenlijia1111.utils.common.Result
     * @since 下午 5:51 2019/11/22 0022
     **/
    public Result customCancelOrder(String groupId) {
        return shoppingOrderBiz.customCancelOrder(groupId);
    }

    /**
     * 商家发货
     *
     * @param params
     * @return com.github.chenlijia1111.utils.common.Result
     * @since 下午 5:51 2019/11/22 0022
     **/
    public Result shopSendExpress(ShipParams params) {
        return shoppingOrderBiz.shopSendExpress(params);
    }

    /**
     * 商家批量发货
     *
     * @param params
     * @return com.github.chenlijia1111.utils.common.Result
     * @since 下午 5:51 2019/11/22 0022
     **/
    @Transactional
    public Result shopBatchSendExpress(BatchShipParams params) {
        return shoppingOrderBiz.shopBatchSendExpress(params);
    }

    /**
     * 客户收货
     *
     * @param orderNo 订单编号
     * @return com.github.chenlijia1111.utils.common.Result
     * @since 下午 5:51 2019/11/22 0022
     **/
    public Result customReceiveExpress(String orderNo) {
        return shoppingOrderBiz.customReceiveExpress(orderNo);
    }


    /**
     * 拼团支付回调
     * 只处理关于普通订单的回调业务,不处理其他不相关业务(拼团、秒杀)
     *
     * @param payRecode     商家支付订单号
     * @param transactionId 第三方支付交易流水号
     * @param payChannel    支付渠道
     * @return void
     * @since 上午 9:57 2019/11/27 0027
     **/
    public void payNotify(String payRecode, String transactionId, String payChannel) {
        shoppingOrderBiz.payNotify(payRecode, transactionId, payChannel);
    }


}
